package alipay.openplatform.edu.campus.jiuye.service.services.system;

import alipay.openplatform.edu.campus.jiuye.common.utils.CopyVoPo;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.system.AreaInfoMapper;
import alipay.openplatform.edu.campus.jiuye.model.po.AreaInfoPO;
import alipay.openplatform.edu.campus.jiuye.model.vo.AreaInfoVO;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import alipay.openplatform.edu.campus.jiuye.service.iservice.system.IAreaInfoService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("AreaInfoService")
public class AreaInfoService implements IAreaInfoService{

    //日志操作类
    private static Logger logger = LoggerFactory.getLogger(AreaInfoService.class);


    //地区DAO对象
    @Autowired
    AreaInfoMapper areaInfoMapper;


    /**
     * 分页获取城市信息 - ZONG
     *
     * @param cityName  城市名称
     * @param pageNo    当前页的编号
     * @param pageSize  每页的行数
     * @return
     */
    public ApiResult getCityPage(@Param("cityName") String cityName, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize) throws  Exception
    {

        //cityName =new String(cityName.getBytes("iso-8859-1"), "utf-8");

        logger.info("根据查询条件分页获取申请单的信息|AreaInfoService|getCityPage|params:cityName={},pageNo={},pageSize={}", cityName, pageNo, pageSize);

        //1. 获取总页数
        int count = areaInfoMapper.getCityInfoCount(cityName);

        //2. 获取当前开始的行的位置
        int startPages = (pageNo - 1) * pageSize;
        if (startPages < 0) startPages = 0;

        //3. 分页获取数据
        List<AreaInfoPO> list = areaInfoMapper.getCityPage(cityName,startPages, pageSize);

        List<AreaInfoVO> listVO = CopyVoPo.copyList(list,AreaInfoVO.class);
        //4. 返回分页数据
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total_num", count);
        result.put("list", listVO);

        return ApiResult.buildSuccess("0", "分页返回数据成功!", result);


    }


    /**
     * 删除城市信息 - ZONG
     *
     * @param areaId  城市ID
     * @return
     */
    public  ApiResult delCity(String areaId)
    {
        int count = areaInfoMapper.deleteByPrimaryKey(areaId);
        if(count > 0)
        {
            return ApiResult.buildSuccess("0", "删除数据成功!",null);
        }
        return ApiResult.buildSuccess("-1", "没有数据可以删除!",null);

    }

    @Override
    public String getCityName(int cityId) {
        return areaInfoMapper.getCityNameById(cityId);
    }

}